package ch.unifr.diva.multimodals.spherex;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.AsyncTask;
import android.widget.Toast;

/**
 * Class to regularly check the connection to the main application (in contrast to the tester in the IP setup interface, where the connection is just created once) 
 * @author Dani
 *
 */
public class Runner extends AsyncTask<NetConfigurationActivity, Integer, Boolean>{
		
	public static enum Reason{UNKNOWN, UNKNOWN_HOST, IOEXCEPTION, NO_INTERNET_CONNECTION, SOCKET_NOT_CONNECTED}

	public static final int SOCKET_TIMEOUT=2000;
	
	private NetConfigurationActivity act;
	private Reason reason;
//	private boolean bound;
//	private boolean connected;
//	private boolean closed;
//	private boolean inputShutDown;
//	private boolean outputShutDown;
	private MultimodalDialog md;
	
	/**
	 * If connection has been checked, proceed with result gotten
	 */
	protected void onPostExecute(Boolean result) {
//		act.appendToConsole(act.getString(R.string.status_heartbeat)+act.getString(result? R.string.status_connected : R.string.status_disconnected));
		if(result){
			//Set connection state accordingly
			act.setEnabledConnection(true, false);
		} else {
			String rs=null;
			switch(reason){
			case NO_INTERNET_CONNECTION:
				rs=act.getString(R.string.status_noInternetConnection);
				break;
			case SOCKET_NOT_CONNECTED:
				rs=act.getString(R.string.status_socketNotConnected);
				break;
			case IOEXCEPTION:
				rs=act.getString(R.string.status_ioException);
				break;
			case UNKNOWN_HOST:
				rs=act.getString(R.string.status_unknownHost);
				break;
			default:
			case UNKNOWN:
				rs=act.getString(R.string.status_reasonUnknown);
			break;
			}
			//Inform the user and set connection state accordingly
			Toast.makeText(act.getApplicationContext(), act.getString(R.string.status_reasonUnknown)+rs,
					Toast.LENGTH_LONG).show();
			act.setEnabledConnection(false, true);
			md.confirm(false, true);
		}
		}

	/**
	 * Actually performing the action
	 */
	@Override
	protected Boolean doInBackground(NetConfigurationActivity... params) {
		act = params[0];
		md=new MultimodalDialog(act);
//		bound=false;
//		connected=false;
//		closed=false;
//		inputShutDown=false;
//		outputShutDown=false;
//		
//		try{
//		bound = act.socket.isBound();
//		connected = act.socket.isConnected();
//		closed = act.socket.isClosed();
//		inputShutDown = act.socket.isInputShutdown();
//		outputShutDown = act.socket.isOutputShutdown();
//		}catch(Exception e){
//		//Must re-establish connection
//		}
//		if(bound && connected && !closed && !inputShutDown && !outputShutDown)
//			//Nothing to do: socket up and running
//			return true;
//		else
		try {
			//Sent test string to main application
			act.socket = new Socket(act.getIp(), act.getPort());
			act.socket.setSoTimeout(SOCKET_TIMEOUT);
	    	//socket = new Socket(ip.getText().toString(), Integer.parseInt(port.getText().toString()));
			act.out = act.socket.getOutputStream();
		    act.output = new PrintWriter(act.out);
		    act.output.print(act.getString(R.string.status_checkConnection));
		    return true;
		} catch (UnknownHostException e) {
			reason=Runner.Reason.UNKNOWN_HOST;
			return false;
		} catch (IOException e) {
			reason=Runner.Reason.IOEXCEPTION;
			return false;
		}
		
	}

	/**
	 * Interrupt test
	 */
	@Override
	protected void onCancelled() {
		super.onCancelled();
	}
	
}
